/*
 * @(#)MainMenu.java    1.0
 *
 * Copyright 2009 GFE, All rights reserved.
 * Game For Education.
 */
package egfe;

import java.io.IOException;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 * @author  Sajarwo Anggai
 * @author  Abdul Rizal Adompo
 * @version 1.0, 11 May 2009
 * @see	    SplashScreen
 * @see	    GFEInfo
 */
public class MainMenu extends GameCanvas implements Runnable, MainMenuAction {

    private Display display;
    private Image backgroundImage;
    private Image imageMenu[];
    private MainMenuAction nextAction[];
    private int currentPos = 0;
    private int divideImageMenu;
    private int xPos;
    private int yPos;
    private int incYpos;
    private long sleepTime;
    private boolean stop;
    public Graphics graphics;

    /**
     * 
     * @param display - Display untuk setting Displayable
     * @param locationBgImage - Lokasi image sebagai background pada main menu
     */
    public MainMenu(Display display, String locationBgImage) {
        super(true);
        setFullScreenMode(true);
        try {
            this.display = display;
            backgroundImage = Image.createImage(locationBgImage);
        } catch (IOException ex) {
        }
        setSleepTime(200);
    }

    /**
     *
     * @param sleepTime setting waktu sleep pada thread dalam satuan milidetik
     */
    public void setSleepTime(long sleepTime) {
        this.sleepTime = sleepTime;
    }

    /**
     *
     * @return waktu sleep thread MainMenu
     */
    public long getSleepTime() {
        return sleepTime;
    }

    /**
     *
     * @param imageMenu list image untuk submenu
     * @param nextDisplayable list displayable submenu
     */
    public void setMenu(Image imageMenu[], MainMenuAction nextAction[]) {
        this.imageMenu = imageMenu;
        this.nextAction = nextAction;
        this.divideImageMenu = this.imageMenu.length / 2;
    }

    /**
     * 
     * @param current menentukan index posisi menu
     */
    public void setCurrentPos(int currentPos) {
        if (currentPos >= 0 && currentPos < imageMenu.length / 2) {
            this.currentPos = currentPos;
        } else {
            currentPos = 0;
        }
    }

    /**
     * Mengembalikan nilai dari menu terkini
     * @return index posisi menu terkini
     */
    public int getCurrentPos() {
        return currentPos;
    }

    /**
     * Setting posisi awal sub menu
     * @param xPos posisi koordinat x
     * @param yPos posisi koordinat y
     */
    public void setPositionSubMenu(int xPos, int yPos) {
        this.xPos = xPos;
        this.yPos = yPos;
    }

    /**
     *
     * @param incYPos jarak antara posisi koordinat y pada masing-masin submenu
     */
    public void setIncY(int incYPos) {
        this.incYpos = incYPos;
    }

    public void draw(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, 0);
        }

        for (int i = 0; i < divideImageMenu; i++) {
            if (imageMenu[i + divideImageMenu] != null) {
                if (currentPos == i) {
                    g.drawImage(imageMenu[i + divideImageMenu], xPos, yPos + (i * incYpos), 0);
                } else {
                    g.drawImage(imageMenu[i], xPos, yPos + (i * incYpos), 0);
                }
            }
        }
    }

    public void run() {
        graphics=getGraphics();
        while (!stop) {
            draw(graphics);
            keyPressed(getKeyStates());
            flushGraphics(0, 0, getWidth(), getHeight());
            try {
                Thread.sleep(sleepTime);
            } catch (InterruptedException ex) {
            }
        }
    }

    protected void keyPressed(int pressed) {
        if (pressed == DOWN_PRESSED || pressed == 56) {
            setCurrentPos(getCurrentPos() + 1);
        } else if (pressed == UP_PRESSED || pressed == 50) {
            setCurrentPos(getCurrentPos() - 1);
        } else if (pressed == FIRE_PRESSED || pressed == 53) {
            nextAction[currentPos].action(0);
        }
    }

    /**
     * Menjalankan main menu
     */
    public void start() {
        new Thread(this).start();
    }

    /**
     *  Menghentikan proses pada MainMenu
     */
    public void stop() {
        stop = true;
    }

    public void action(int action) {
        setFullScreenMode(true);
        display.setCurrent(this);
        start();
    }
}
